//
//  NewsDetailContent.m
//
//  Created by lizhenqiang  on 16/4/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "NewsDetailContent.h"


NSString *const kNewsDetailContentType = @"type";
NSString *const kNewsDetailContentIdx = @"idx";
NSString *const kNewsDetailContentContent = @"content";


@interface NewsDetailContent ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation NewsDetailContent

@synthesize type = _type;
@synthesize idx = _idx;
@synthesize content = _content;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.type = [[self objectOrNilForKey:kNewsDetailContentType fromDictionary:dict] doubleValue];
            self.idx = [[self objectOrNilForKey:kNewsDetailContentIdx fromDictionary:dict] doubleValue];
            self.content = [self objectOrNilForKey:kNewsDetailContentContent fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.type] forKey:kNewsDetailContentType];
    [mutableDict setValue:[NSNumber numberWithDouble:self.idx] forKey:kNewsDetailContentIdx];
    [mutableDict setValue:self.content forKey:kNewsDetailContentContent];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.type = [aDecoder decodeDoubleForKey:kNewsDetailContentType];
    self.idx = [aDecoder decodeDoubleForKey:kNewsDetailContentIdx];
    self.content = [aDecoder decodeObjectForKey:kNewsDetailContentContent];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeDouble:_type forKey:kNewsDetailContentType];
    [aCoder encodeDouble:_idx forKey:kNewsDetailContentIdx];
    [aCoder encodeObject:_content forKey:kNewsDetailContentContent];
}

- (id)copyWithZone:(NSZone *)zone
{
    NewsDetailContent *copy = [[NewsDetailContent alloc] init];
    
    if (copy) {

        copy.type = self.type;
        copy.idx = self.idx;
        copy.content = [self.content copyWithZone:zone];
    }
    
    return copy;
}


@end
